# from django.shortcuts import render, redirect,get_object_or_404
# from django.views.generic import ListView,UpdateView
# from .models import KeHu,kehu_gonghai,KeHugenjinlog
# from .forms import KeHu_zhuangonghai_Form
# from django.http import JsonResponse
# from datetime import datetime
#
#
# class gonghai_listview(ListView):
#     model = KeHu
#     template_name = "gonghaitemp/kehu_gonghai_list.html"
#     context_object_name = 'kehu_gonghai_list'
#     bigtitle = "客户公海"
#     paginate_by = 20
#     ordering = ['-addtime']# 按照id降序排列
#     def get_queryset(self):
#         # 获取当前登录的用户
#         current_user = self.request.user
#         if current_user.is_superuser:
#             return KeHu.objects.filter(if_gonghai=1).order_by('-addtime')
#         else:
#             return KeHu.objects.filter(admin_user=current_user, if_gonghai=1).order_by('-addtime')  # 过滤出当前用户的数据
#         return queryset
#     def get_context_data(self, **kwargs):
#         context = super().get_context_data(**kwargs)
#         context['bigtitle'] = self.bigtitle
#         # 添加表单实例到上下文
#         context['KeHu_zhuangonghai_Form'] = KeHu_zhuangonghai_Form()
#         # 分页
#         page = context['page_obj']
#         context['has_previous'] = page.has_previous()
#         context['has_next'] = page.has_next()
#         context['previous_page_number'] = page.previous_page_number() if page.has_previous() else None
#         context['next_page_number'] = page.next_page_number() if page.has_next() else None
#         return context
#
# class Gonghai_KehuUpdateView(UpdateView):
#         model = KeHu
#         form_class = KeHu_zhuangonghai_Form
#         template_name = 'gonghaitemp/gonghai_zhuangtai_up.html'
#         #success_url = 'kehu_gonghai_list'
#
#         def form_valid(self, form):
#             # 保存表单等操作...
#             print(form.cleaned_data)
#             self.object = form.save()
#
#             try:
#                 kehu_gonghai = self.object.kehu_gonghai_related # 获取关联的KeHu_gonghai实例
#             except kehu_gonghai.DoesNotExist:
#                 # 如果没有找到关联的KeHu_gonghai实例，可以选择创建一个新的或者返回错误
#                 kehu_gonghai = kehu_gonghai.objects.create(kehu=self.object)
#
#            # 更新lingqushu字段的值
#             current_lingqushu_value = kehu_gonghai.lingqushu
#             current_lingqushu_value_int = int(current_lingqushu_value)
#             current_zuihoulingqu_user=str(self.request.user.id)
#
#             kehu_gonghai.lingqushu = current_lingqushu_value_int + 1
#             kehu_gonghai.lingqutime = datetime.now()
#             kehu_gonghai.zuihoulingqu_user = current_zuihoulingqu_user
#             kehu_gonghai.save()  # 保存更新后的KeHu_gonghai实例
#             # 创建新的KeHugenjinlog 更新客户跟进实例
#             kehu_genjinlog = KeHugenjinlog.objects.create(kehu=self.object)
#             kehu_genjinlog.genjinleixing=1
#             kehu_genjinlog.genjintext=f'<a href="/kehu/{current_zuihoulingqu_user}/detail/">xxx </a>/从客户公海领取'
#             kehu_genjinlog.genjintime=datetime.now()
#             kehu_genjinlog.save()  # 保存更新后的KeHu_gonghai实例
#             # 返回一个JsonResponse给Ajax调用者
#             response_data = {'message': '表单提交成功！', 'status': 'success'}  # 自定义返回信息 status -》success 告诉前端提交成功
#             return JsonResponse(response_data)  # 返回JSON响应而不是重定向
#
#         def form_invalid(self, form):
#             # 处理无效的表单提交情况...
#             response_data = {'errors': form.errors, 'status': 'failure'}  # 自定义返回信息 status -》failure 告诉前端提交失败
#             return JsonResponse(response_data, status=400)
#
